package org.jeecg.modules.extbpm.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * JSONHelper.java
 */
@Slf4j
public final class JSONHelper {

	
	  /** 
	    * 将json格式的字符串解析成Map对象 <li> 
	    * json格式：{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff","data":{"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff"}} 
	    */  
	   public static Map<String, Object> json2Map(String jsonStr)  {  
	       Map<String, Object> data = new HashMap<String, Object>();  
	       // 将json字符串转换成jsonObject  
	       JSONObject jsonObject = JSONObject.parseObject(jsonStr);  
	       for (Object obj : jsonObject.entrySet()){  
	    	   String key = (String)((Map.Entry)obj).getKey();  
	           Object value = ((Map.Entry)obj).getValue();  
	           data.put(key, value); 
	       }
	       return data;  
	   }  
	   
   /**去除大字段  去除 对象和数组
	 * @param jsonStr
	 * @return
	 */
	public static Map<String, Object> json2MapNoObj(String jsonStr)  {  
	       Map<String, Object> data = new HashMap<String, Object>();
	       // 将json字符串转换成jsonObject
	       JSONObject jsonObject = JSONObject.parseObject(jsonStr);  
	       for (Object obj : jsonObject.entrySet()){  
	    	   String key = (String)((Map.Entry)obj).getKey(); 
	    	   Object valObj = ((Map.Entry)obj).getValue();
	    	   if(valObj instanceof JSONObject ||valObj instanceof JSONArray) {
	    		   continue;
	    	   }
	           String value = ooConvertUtils.getString(valObj);
	           if(value!=null && value.length()>1000) {
	        	   continue;
	           }
	           data.put(key, valObj);
	       }
	       return data;  
	   }  
	   
	   
	   public static void main(String[] args) {
		String str = "{\"name\":\"admin\",\"retries\":222,\"testname\":\"ddd\",\"testretries\":\"fffffffff\",\"data\":{\"name\":\"admin\",\"retries\":\"3fff\",\"testname\":\"ddd\",\"testretries\":\"fffffffff\"}} ";
		Map<String, Object> data = json2MapNoObj(str);
		System.out.println(data);
	   }
	  
}
